#!/usr/bin/env python

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from widgets.FrameNFSBase import Ui_FrameNFS

class FrameNFS(Ui_FrameNFS, QFrame):
    def __init__(self, connection, parent = None):
        super(FrameNFS, self).__init__(parent)
        self.setupUi(self)

        self.NFS = connection.root.Servers.NFS.NFS()
        self.reload()

    def formatExports(self, exportsList):
        optionsDict = {'ro':'Read-Only', 'rw':'Read-Write', \
                       'root_squash':'Root Squash', \
                       'no_root_squash':'No Root Squashing', \
                       'sync':'Synchronous', 'async':'Asynchronous'}

        newList = []
        for export in exportsList :
            options = export[-1]
            newOptions = [ optionsDict[op] for op in options ]
            newRule = ','.join(newOptions) + '\t' + '\t'.join(export[:-1])
            newList.append(newRule)

        return newList

    def reload(self):
        self.exportsList = self.NFS.getExports()
        tempList = self.formatExports(self.exportsList)

        self.lstMain.clear()
        self.lstMain.addItems(tempList)


    @pyqtSlot()
    def on_btnAdd_clicked(self):
        addDialog = AddExport(self)
        addDialog.show()

    @pyqtSlot()
    def on_btnReload_clicked(self):
        self.reload()

    @pyqtSlot()
    def on_btnDel_clicked(self):
        currentIndex = self.lstMain.currentRow()
        currentExport = self.exportsList[currentIndex][0]

        if self.NFS.delExport(currentExport) :
            self.reload()
        else :
            QMessageBox.critical(self, "Error!", "Error while deleting this export.")




class AddExport(QDialog):
    def __init__(self, parent = None):
        super(AddExport, self).__init__(parent)

        self.parentObject = parent

        self.lblPath = QLabel('Directory Location')
        self.txtPath = QLineEdit()
        self.lblClient = QLabel('Client')
        self.txtClient = QLineEdit()
        self.chkReadWrite = QCheckBox('Read-Write')
        self.chkNoRootSquash = QCheckBox('No root squashing')
        self.chkAsync = QCheckBox('Asynchronous')
        self.btnAdd = QPushButton('Apply')

        self.vlPath = QVBoxLayout()
        self.vlPath.addWidget(self.lblPath)
        self.vlPath.addWidget(self.txtPath)

        self.vlClient = QVBoxLayout()
        self.vlClient.addWidget(self.lblClient)
        self.vlClient.addWidget(self.txtClient)

        self.hlOptions = QHBoxLayout()
        self.hlOptions.addWidget(self.chkReadWrite)
        self.hlOptions.addWidget(self.chkNoRootSquash)
        self.hlOptions.addWidget(self.chkAsync)
        self.spApply = QSpacerItem(20, 40, QSizePolicy.Expanding, QSizePolicy.Minimum)
        self.hlOptions.addItem(self.spApply)
        self.hlOptions.addWidget(self.btnAdd)

        self.hlExportGroup = QHBoxLayout()
        self.hlExportGroup.addLayout(self.vlPath)
        self.hlExportGroup.addLayout(self.vlClient)

        self.spMain = QSpacerItem(20, 20, QSizePolicy.Minimum, QSizePolicy.Expanding)
        self.vlMain = QVBoxLayout()
        self.vlMain.addLayout(self.hlExportGroup)
        self.vlMain.addItem(self.spMain)
        self.vlMain.addLayout(self.hlOptions)

        self.setLayout(self.vlMain)

        self.setWindowTitle('Add Export')

        self.connect(self.btnAdd, SIGNAL('clicked()'), self.apply)

    def apply(self):
        path = str(self.txtPath.text())
        client = str(self.txtClient.text())
        options = []

        if self.chkReadWrite.isChecked() :
            options.append('rw')
        else :
            options.append('ro')

        if self.chkAsync.isChecked() :
            options.append('async')

        if self.chkNoRootSquash.isChecked() :
            options.append('no_root_squash')

        if len(path) == 0 or len(client) == 0 :
            QMessageBox.critical(self, "Error!", "Please fill in the required fields.")
            return

        if self.parentObject.NFS.addExport(path, client, options) :
            self.parentObject.reload()
            self.close()


